package com.pmt.jnipractice
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class ImageProcessActivity : AppCompatActivity() {

    private lateinit var originalImageView: ImageView
    private lateinit var blurredImageView: ImageView
    private lateinit var radiusSeekBar: SeekBar
    private lateinit var radiusTextView: TextView
    private lateinit var processButton: Button

    private var currentRadius = 5

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image_process)

        initViews()
        setupListeners()
        loadSampleImage()
    }

    private fun initViews() {
        originalImageView = findViewById(R.id.originalImageView)
        blurredImageView = findViewById(R.id.blurredImageView)
        radiusSeekBar = findViewById(R.id.radiusSeekBar)
        radiusTextView = findViewById(R.id.radiusTextView)
        processButton = findViewById(R.id.processButton)
    }

    private fun setupListeners() {
        radiusSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                currentRadius = progress
                radiusTextView.text = "Blur Radius: $currentRadius"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
        })

        processButton.setOnClickListener {
            applyGaussianBlur()
        }
    }

    private fun loadSampleImage() {
        // 创建一个简单的彩色样本图像
        val bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888)
        val canvasSize = 200

        for (x in 0 until canvasSize) {
            for (y in 0 until canvasSize) {
                val color = when {
                    x < canvasSize/3 -> Color.RED
                    x < 2*canvasSize/3 -> Color.GREEN
                    else -> Color.BLUE
                }
                bitmap.setPixel(x, y, color)
            }
        }

        originalImageView.setImageBitmap(bitmap)
    }

    private fun applyGaussianBlur() {
        val originalBitmap = (originalImageView.drawable as? android.graphics.drawable.BitmapDrawable)?.bitmap
        if (originalBitmap != null) {
            val blurredBitmap = performGaussianBlur(originalBitmap, currentRadius)
            blurredImageView.setImageBitmap(blurredBitmap)
        }
    }

    private fun performGaussianBlur(bitmap: Bitmap, radius: Int): Bitmap {
        val width = bitmap.width
        val height = bitmap.height

        // 将Bitmap转换为灰度字节数组
        val grayBytes = bitmapToGrayByteArray(bitmap)

        // 调用Native方法进行高斯模糊处理
        val blurredBytes = NativeImageUtils.applyGaussianBlur(grayBytes, width, height, radius)

        // 将处理后的字节数组转换回Bitmap
        return byteArrayToBitmap(blurredBytes, width, height)
    }

    private fun bitmapToGrayByteArray(bitmap: Bitmap): ByteArray {
        val width = bitmap.width
        val height = bitmap.height
        val bytes = ByteArray(width * height)

        for (y in 0 until height) {
            for (x in 0 until width) {
                val pixel = bitmap.getPixel(x, y)
                // 转换为灰度值
                val r = Color.red(pixel)
                val g = Color.green(pixel)
                val b = Color.blue(pixel)
                val gray = (0.299 * r + 0.587 * g + 0.114 * b).toInt()
                bytes[y * width + x] = gray.toByte()
            }
        }

        return bytes
    }

    private fun byteArrayToBitmap(bytes: ByteArray, width: Int, height: Int): Bitmap {
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)

        for (y in 0 until height) {
            for (x in 0 until width) {
                val grayValue = bytes[y * width + x].toInt() and 0xFF
                val color = Color.rgb(grayValue, grayValue, grayValue)
                bitmap.setPixel(x, y, color)
            }
        }

        return bitmap
    }
}
